package com.lmt.ymcc.interceptor;


import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * @Description: 微服务之间的调用，服务权限的传递拦截器
 * @Author: Amazing
 * @Date: 2022/8/27 19:30
 * @Version: 1.0
 */
@Component
public class TokenFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 将请求头中的token拿出来
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        // 获取到token
        String authorization = request.getHeader("Authorization");
        // 放到feign的请求头中
        if(StringUtils.hasLength(authorization)) {
            requestTemplate.header("Authorization",authorization);
        }
    }
}
